﻿Imports Microsoft.Reporting.WinForms

Public Class VerGuia

    Public idGuia As Integer
    Private oGuia As New CGuia
    Private oMaestra As New CMaestras
    Private Const CODIGOIATA As String = "7610507"
    Private Const DECLAREDVALUE1 As String = "NVD"
    Private Const DECLAREDVALUE2 As String = "NVC"
    Private Const AMOUNTINSURANCE As String = "NIL"
    Private Const SIGNATURE As String = "ZARATRANS S.A.S. AS AGENT OF ZIX CORPORATION"


    Private Sub VerGuia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim ds As New DataSet
            ds = Me.oGuia.GetInfoGuia(Me.idGuia)

            If (ds.Tables(0).Rows.Count > 0) Then
                ds.Tables(0).Rows(0)("CODIGOIATA") = CODIGOIATA
                ds.Tables(0).Rows(0)("DECLAREDVALUE1") = DECLAREDVALUE1
                ds.Tables(0).Rows(0)("DECLAREDVALUE2") = DECLAREDVALUE2
                ds.Tables(0).Rows(0)("AMOUNTINSURANCE") = AMOUNTINSURANCE
                ds.Tables(0).Rows(0)("SIGNATURE") = SIGNATURE
                ds.Tables(0).Rows(0)("AGENTE") = ds.Tables(0).Rows(0)("AIRWAYBILL").ToString()

                'Columna para guardar el total de los items
                ds.Tables(0).Columns.Add("TOTALITEMS")

                'Calcula totales
                Dim totalDueAgent As Double = 0
                Dim totalDueCarrier As Double = 0
                Dim totalCollect As Double = 0
                Dim collect As Double = 0
                Dim textoRata As String = ""
                Dim totalGrossWeightXFS2 As Double = 0

                'calcula y suma totales de los ítems
                'En TOTAL se calcula: Chargeable weight * rata
                'En TOTAL2 se calcula: Chargeable weight * FS
                ds.Tables(1).Columns.Add("TOTAL")
                ds.Tables(1).Columns.Add("TOTAL2")
                ds.Tables(1).Columns.Add("TEXTOTOTAL")
                Dim totalItems As Double = 0
                For i As Integer = 0 To ds.Tables(1).Rows.Count - 1
                    If (ds.Tables(1).Rows(i)("UNIDAD").ToString() = "KILO") Then
                        ds.Tables(1).Rows(i)("UNIDAD") = "KG"
                    Else
                        ds.Tables(1).Rows(i)("UNIDAD") = "LB"
                    End If

                    'Se calcula la columna TOTAL
                    ds.Tables(1).Rows(i)("TOTAL") = CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("RATA").ToString())
                    collect += CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("RATA").ToString())
                    totalItems += CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("RATA").ToString())
                    ds.Tables(1).Rows(i)("TEXTORATA") = ds.Tables(1).Rows(i)("RATA").ToString()

                    'Se calcula la columna TOTAL2
                    'Si hay un valor de FS diferente a uno se muestra en el campo de rata, y se tiene
                    'en cuenta en el cálculo del total                    
                    If (ds.Tables(1).Rows(i)("FS").ToString() <> "1") And (ds.Tables(1).Rows(i)("FS").ToString().Length > 0) Then
                        ds.Tables(1).Rows(i)("TOTAL2") = CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("FS").ToString())
                        collect += CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("FS").ToString())
                        totalItems += CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("FS").ToString())
                        ds.Tables(1).Rows(i)("TEXTORATA") = ds.Tables(1).Rows(i)("TEXTORATA") & Chr(10) & ds.Tables(1).Rows(i)("FS").ToString()
                    End If

                    'Se va sumando el total de grossweight x fs2                    
                    If (ds.Tables(1).Rows(i)("FS2").ToString() <> "1") And (ds.Tables(1).Rows(i)("FS2").ToString().Length > 0) Then
                        totalGrossWeightXFS2 += CDbl(ds.Tables(1).Rows(i)("PESO").ToString()) * CDbl(ds.Tables(1).Rows(i)("FS2").ToString())
                    End If

                    ds.Tables(1).Rows(i)("TEXTOTOTAL") = ds.Tables(1).Rows(i)("TOTAL").ToString() & Chr(10) & ds.Tables(1).Rows(i)("TOTAL2").ToString()
                Next
                ds.Tables(0).Rows(0)("TOTALITEMS") = totalItems

                'Suma los valores de los conceptos
                Dim textoOtrosCargos As String = ""
                Dim valor As Double = 0
                Dim fs2Concepto = 0
                For i As Integer = 0 To ds.Tables(2).Rows.Count - 1                    
                    valor = CDbl(ds.Tables(2).Rows(i)("VALOR").ToString()) + CDbl(ds.Tables(2).Rows(i)("IVA").ToString())                    
                    If (ds.Tables(2).Rows(i)("FS2").ToString() <> "") Then
                        fs2Concepto += CDbl(ds.Tables(2).Rows(i)("FS2").ToString())
                    End If
                    If (ds.Tables(2).Rows(i)("DUEAGENT").ToString() = "1") Then
                        totalDueAgent += valor
                    Else
                        totalDueCarrier += valor
                    End If

                    textoOtrosCargos += ds.Tables(2).Rows(i)("CONCEPTO") & ":" & valor.ToString() & "  "
                Next
                totalCollect = totalDueAgent + totalDueCarrier + collect + fs2Concepto + totalGrossWeightXFS2

                
                'Subtotales y totales de plata
                If (ds.Tables(0).Rows(0)("TIPO").ToString() = "COLLECT") Then
                    ds.Tables(0).Rows(0)("COLLECT") = collect
                    ds.Tables(0).Rows(0)("TOTALCOLLECT") = totalCollect
                    ds.Tables(0).Rows(0)("PREPAID") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALPREPAID") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALDUEAGENT") = totalDueAgent
                    ds.Tables(0).Rows(0)("TOTALDUECARRIER") = totalDueCarrier
                    ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2") = totalGrossWeightXFS2
                    If totalGrossWeightXFS2 = 0 Then
                        ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2") = DBNull.Value
                    End If
                    ds.Tables(0).Rows(0)("TOTALDUEAGENTPREPAID") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALDUECARRIERPREPAID") = DBNull.Value
                    ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2PREPAID") = DBNull.Value
                Else
                    ds.Tables(0).Rows(0)("COLLECT") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALCOLLECT") = DBNull.Value
                    ds.Tables(0).Rows(0)("PREPAID") = collect
                    ds.Tables(0).Rows(0)("TOTALPREPAID") = totalCollect
                    ds.Tables(0).Rows(0)("TOTALDUEAGENT") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALDUECARRIER") = DBNull.Value
                    ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2") = DBNull.Value
                    ds.Tables(0).Rows(0)("TOTALDUEAGENTPREPAID") = totalDueAgent
                    ds.Tables(0).Rows(0)("TOTALDUECARRIERPREPAID") = totalDueCarrier
                    ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2PREPAID") = totalGrossWeightXFS2
                    If totalGrossWeightXFS2 = 0 Then
                        ds.Tables(0).Rows(0)("GROSSWEIGHTXFS2PREPAID") = DBNull.Value
                    End If

                End If


                'Agrega columna para almacenar la informacion de "Otros cargos"
                ds.Tables(0).Columns.Add("CONCEPTOS")
                ds.Tables(0).Rows(0)("CONCEPTOS") = textoOtrosCargos

                'Se trae la información detallada de los clientes
                Dim dsCliente1 As New DataSet
                Dim dsCliente2 As New DataSet
                dsCliente1 = Me.oMaestra.GetClienteById(CInt(ds.Tables(0).Rows(0)("IDCLIENTEEMPRESACARGA")))
                dsCliente2 = Me.oMaestra.GetClienteById(CInt(ds.Tables(0).Rows(0)("IDCLIENTE")))
                If (dsCliente1.Tables(0).Rows.Count > 0) Then
                    ds.Tables(0).Rows(0)("EMPRESACARGA") = dsCliente1.Tables(0).Rows(0)("NOMBRE") & Chr(13) & " " & dsCliente1.Tables(0).Rows(0)("DIRECCION") & Chr(13) & " PHONE: " & dsCliente1.Tables(0).Rows(0)("TELEFONO") & Chr(13) & " " & dsCliente1.Tables(0).Rows(0)("CIUDAD")
                End If
                If (dsCliente2.Tables(0).Rows.Count > 0) Then
                    ds.Tables(0).Rows(0)("CLIENTE") = dsCliente2.Tables(0).Rows(0)("NOMBRE") & Chr(13) & " " & dsCliente2.Tables(0).Rows(0)("DIRECCION") & Chr(13) & " PHONE: " & dsCliente2.Tables(0).Rows(0)("TELEFONO") & Chr(13) & " " & dsCliente2.Tables(0).Rows(0)("CIUDAD")
                End If


                Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
                Dim rep As LocalReport = Me.ReportViewer1.LocalReport
                rep.ReportEmbeddedResource = "Facturacion.Guia.rdlc"

                Dim rptDataSource As New ReportDataSource
                rptDataSource.Name = "dsGuia_GUIA"
                rptDataSource.Value = ds.Tables(0)

                Dim rptDsItems As New ReportDataSource
                rptDsItems.Name = "dsGuia_ITEMS"
                rptDsItems.Value = ds.Tables(1)

                Dim rptDsConceptos As New ReportDataSource
                rptDsConceptos.Name = "dsGuia_CONCEPTOS"
                rptDsConceptos.Value = ds.Tables(2)

                rep.DataSources.Clear()
                rep.DataSources.Add(rptDataSource)
                rep.DataSources.Add(rptDsItems)
                rep.DataSources.Add(rptDsConceptos)
                Me.ReportViewer1.LocalReport.Refresh()
                Me.ReportViewer1.Visible = True
            End If
            Me.ReportViewer1.RefreshReport()

        Catch ex As Exception
            MessageBox.Show("No se puede mostrar el reporte: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub


End Class